package com.site.wisdom_construction.controller.scheduleModule;

import com.site.wisdom_construction.entity.personnelModule.Project;
import com.site.wisdom_construction.entity.scheduleModule.ProjectProgressPlan;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.personnelModule.ProjectService;
import com.site.wisdom_construction.service.scheduleModule.ProgressPlanService;
import com.site.wisdom_construction.service.scheduleModule.ProjectProgressPlanService;
import com.site.wisdom_construction.util.tree.ProjectProgressPlanTreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author daiyifei @Date 2022/1/16 @Description
 */
@Api(tags = "项目进度计划信息接口")
@RestController
@RequestMapping("ProjectProgressPlan")
@CrossOrigin
public class ProjectProgressPlanController {
  @Autowired private ProjectProgressPlanService projectProgressPlanService;

  @Autowired private ProjectService projectService;

  @Autowired
  private ProgressPlanService progressPlanService;

  @ApiOperation(value = "增加项目进度计划信息", notes = "测试数据:")
  @PostMapping("/insertProjectProgressPlan")
  public JsonResult insertProjectProgressPlan(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    // 查询整体项目信息
    Project project = projectService.findProjectByProjectId(map);
    System.out.println("project:" + project);
    String scheduledCommencementDate = String.valueOf(project.getScheduledCommencementDate());
    System.out.println("scheduledCommencementDate:" + scheduledCommencementDate);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
      // 进度计划开始时间毫秒数
      long projectStartTimes =
          simpleDateFormat.parse((String) map.get("projectStartTime")).getTime();
      // 进度计划结束时间毫秒数
      long projectEndTimes = simpleDateFormat.parse((String) map.get("projectEndTime")).getTime();
      // 项目计划施工天数
      int projectDuration =
          (int) ((projectEndTimes - projectStartTimes) / (24 * 60 * 60 * 1000)) + 1;
      map.put("projectDuration", projectDuration);

      // 整体项目计划开工日期毫秒数
      long scheduledCommencementDateTimes =
          simpleDateFormat.parse(scheduledCommencementDate).getTime();
      int projectStartDay =
          (int) ((projectStartTimes - scheduledCommencementDateTimes) / (24 * 60 * 60 * 1000));
      int projectEndDay =
          (int) ((projectEndTimes - scheduledCommencementDateTimes) / (24 * 60 * 60 * 1000));
      map.put("projectStartDay", projectStartDay);
      map.put("projectEndDay", projectEndDay);
    } catch (Exception e) {
      e.printStackTrace();
    }
    int i = projectProgressPlanService.insertProjectProgressPlan(map);
    if (i == 1) {
      projectProgressPlanService.updateProjectProgressPlanByTrue(map);
      jsonResult.setCode(200);
      jsonResult.setMessage("成功录入项目进度计划信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("录入项目进度计划信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "删除项目进度计划信息", notes = "测试数据:")
  @PostMapping("/deleteProjectProgressPlan")
  public JsonResult deleteProjectProgressPlan(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    int a = projectProgressPlanService.deleteProjectProgressPlan(map);
    if (a == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功删除项目进度计划信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("删除项目进度计划信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "修改项目进度计划信息", notes = "测试数据:")
  @PostMapping("/updateProjectProgressPlan")
  public JsonResult updateProjectProgressPlan(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    // 查询整体项目信息
//    Project project = projectService.findProjectByProjectId(map);
//    String scheduledCommencementDate = String.valueOf(project.getScheduledCommencementDate());
//    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//    try {
//      // 进度计划开始时间毫秒数
//      long projectStartTimes =
//          simpleDateFormat.parse((String) map.get("projectStartTime")).getTime();
//      // 进度计划结束时间毫秒数
//      long projectEndTimes = simpleDateFormat.parse((String) map.get("projectEndTime")).getTime();
//      // 项目计划施工天数
//      int projectDuration =
//          (int) ((projectEndTimes - projectStartTimes) / (24 * 60 * 60 * 1000)) + 1;
//      map.put("projectDuration", projectDuration);
//
//      // 整体项目计划开工日期毫秒数
//      long scheduledCommencementDateTimes =
//          simpleDateFormat.parse(scheduledCommencementDate).getTime();
//      int projectStartDay =
//          (int) ((projectStartTimes - scheduledCommencementDateTimes) / (24 * 60 * 60 * 1000));
//      int projectEndDay =
//          (int) ((projectEndTimes - scheduledCommencementDateTimes) / (24 * 60 * 60 * 1000));
//      map.put("projectStartDay", projectStartDay);
//      map.put("projectEndDay", projectEndDay);
//    } catch (Exception e) {
//      e.printStackTrace();
//    }
    int i = projectProgressPlanService.updateProjectProgressPlan(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功修改项目进度计划信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("修改项目进度计划信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "项目进度计划完成", notes = "测试数据:")
  @PostMapping("/updateProjectProgressPlanByStatus")
  public JsonResult updateProjectProgressPlanByStatus(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();

    // 查询当前id的项目进度计划信息
    ProjectProgressPlan projectProgressPlan =
        projectProgressPlanService.findProjectProgressPlanById(map);
    String projectStartTime = String.valueOf(projectProgressPlan.getProjectStartTime());
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    String nowTime = simpleDateFormat.format(date);
    try {
      // 进度计划开始时间毫秒数
      long projectStartTimes = simpleDateFormat.parse(projectStartTime).getTime();
      // 进度实际结束时间毫秒数
      long projectPracticalTimeTimes = simpleDateFormat.parse(nowTime).getTime();
      // 项目计划施工天数
      int projectPractical =
          (int) ((projectPracticalTimeTimes - projectStartTimes) / (24 * 60 * 60 * 1000)) + 1;
      if (projectPractical <= 0) {
        map.put("projectPractical", 0);
      } else {
        map.put("projectPractical", projectPractical);
      }
      map.put("projectPracticalTime", nowTime);
    } catch (Exception e) {
      e.printStackTrace();
    }
    int i = projectProgressPlanService.updateProjectProgressPlanByStatus(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("操作成功!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("操作失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "分页模糊查询项目进度计划信息", notes = "测试数据:")
  @PostMapping("/findProjectProgressPlanByPage")
  public Page findProjectProgressPlanByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(projectProgressPlanService.Total(map));
    page.setItems(projectProgressPlanService.findProjectProgressPlanByPage(map));
    return page;
  }

  @ApiOperation(value = "下拉框查询全部进度计划信息", notes = "测试数据:")
  @PostMapping("/selectProjectProgressPlan")
  public OneDataResult selectProjectProgressPlan(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(projectProgressPlanService.selectProjectProgressPlan(map));
    return oneDataResult;
  }

  @ApiOperation(value = "进度树形图", notes = "测试数据:")
  @PostMapping("/projectProgressPlanTree")
  public OneDataResult projectProgressPlanTree(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    map.put("fatherId", map.get("id"));
    //编制计划id数组
    List list = new ArrayList();
    int id = (int) map.get("id");
    List arrayList = new ArrayList();
    list = menuListl(id, arrayList);
    list.add(id);
    map.put("list",list);
    oneDataResult.setData(projectProgressPlanService.findProjectProgressPlanByProjectId(map));
    return oneDataResult;
  }

  public List menuListl(int id, List arrayList) {
    List list = progressPlanService.findProgressPlanByFatherId(id);
    if (list.size() == 0) {
      return arrayList;
    } else {
      for (int i = 0; i < list.size(); i++) {
        arrayList.add(list.get(i));
        menuListl((Integer) list.get(i), arrayList);
      }
      return arrayList;
    }
  }
}
